﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Models
{
    static public class CompareStoreModel
    {
        //lấy danh sách procedure
        static public DataTable ProcedureList(DataModel conn, string BeginDate, string EndDate)
        {
            DataTable tb = new DataTable();
            //string query = FilesModel.RealAllText("Get_store.sql");
            string query = @"SELECT name,create_date,modify_date,'' AS content
                                FROM sys.procedures
                                WHERE modify_date BETWEEN '{0}' AND '{1}'
                                ORDER BY modify_date desc;";

            if (string.IsNullOrEmpty(query))
                return tb;
            try
            {
                tb = conn.QueryResults(string.Format(query, BeginDate, EndDate)).Tables[0];
            }
            catch
            {
                return new DataTable();
            }

            return tb;
        }

        //lấy danh sách procedure
        static public DataTable ProcedureList(DataModel conn)
        {
            DataTable tb = new DataTable();
            //string query = FilesModel.RealAllText("Get_store.sql");
            string query = @"SELECT name,create_date,modify_date,'' AS content
                                FROM sys.procedures
                                ORDER BY modify_date desc;";

            if (string.IsNullOrEmpty(query))
                return tb;
            try
            {
                tb = conn.QueryResults(query).Tables[0];
            }
            catch
            {
                return new DataTable();
            }

            return tb;
        }

        //Lấy danh sách function
        static public DataTable FunctionList(DataModel conn, string BeginDate, string EndDate)
        {
            DataTable tb = new DataTable();
            string query = FilesModel.RealAllText("Get_Func.sql");
            if (string.IsNullOrEmpty(query))
                return tb;
            try
            {
                tb = conn.QueryResults(string.Format(query, BeginDate, EndDate)).Tables[0];
            }
            catch
            {
                return new DataTable();
            }
            return tb;
        }
        //static public void GetAllStored(DataModel conn, ref DataTable tb)
        //{
        //    string replaceText = @"CREATE PROCEDURE";

        //    string query = FilesModel.RealAllText("Get_store.sql");
        //    string query1 = string.Empty;
        //    int n = tb.Rows.Count;
        //    for (int i = 0; i < n; i++)
        //    {
        //        query1 = string.Format(query, tb.Rows[i]["name"]);
        //        DataTable _tb = conn.QueryResults(query1).Tables[0];
        //        string s = string.Empty;
        //        foreach (DataRow r in _tb.Rows)
        //            s += r[0].ToString();
        //        s = s.ToUpper();
        //        tb.Rows[i]["content"] = s;
        //    }
        //}
    }
}
